{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7a5c2153",
   "metadata": {},
   "source": [
    "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/langchain-ai/langchain-academy/blob/main/module-4/research-assistant.ipynb) [![Open in LangChain Academy](https://cdn.prod.website-files.com/65b8cd72835ceeacd4449a53/66e9eba12c7b7688aa3dbb5e_LCA-badge-green.svg)](https://academy.langchain.com/courses/take/intro-to-langgraph/lessons/58239974-lesson-4-research-assistant)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e0a5763f-5f45-4b8f-b3e2-480f46c5721b",
   "metadata": {},
   "source": [
    "# Research Assistant\n",
    "\n",
    "## Review\n",
    "\n",
    "We've covered a few major LangGraph themes:\n",
    "\n",
    "* Memory\n",
    "* Human-in-the-loop\n",
    "* Controllability\n",
    "\n",
    "Now, we'll bring these ideas together to tackle one of AI's most popular applications: research automation. \n",
    "\n",
    "Research is often laborious work offloaded to analysts. AI has considerable potential to assist with this.\n",
    "\n",
    "However, research demands customization: raw LLM outputs are often poorly suited for real-world decision-making workflows. \n",
    "\n",
    "Customized, AI-based [research and report generation](https://jxnl.co/writing/2024/06/05/predictions-for-the-future-of-rag/#reports-over-rag) workflows are a promising way to address this.\n",
    "\n",
    "## Goal\n",
    "\n",
    "Our goal is to build a lightweight, multi-agent system around chat models that customizes the research process.\n",
    "\n",
    "`Source Selection` \n",
    "* Users can choose any set of input sources for their research.\n",
    "  \n",
    "`Planning` \n",
    "* Users provide a topic, and the system generates a team of AI analysts, each focusing on one sub-topic.\n",
    "* `Human-in-the-loop` will be used to refine these sub-topics before research begins.\n",
    "  \n",
    "`LLM Utilization`\n",
    "* Each analyst will conduct in-depth interviews with an expert AI using the selected sources.\n",
    "* The interview will be a multi-turn conversation to extract detailed insights as shown in the [STORM](https://github.com/langchain-ai/langgraph/blob/main/examples/storm/storm.ipynb) paper.\n",
    "* These interviews will be captured in a using `sub-graphs` with their internal state. \n",
    "   \n",
    "`Research Process`\n",
    "* Experts will gather information to answer analyst questions in `parallel`.\n",
    "* And all interviews will be conducted simultaneously through `map-reduce`.\n",
    "\n",
    "`Output Format` \n",
    "* The gathered insights from each interview will be synthesized into a final report.\n",
    "* We'll use customizable prompts for the report, allowing for a flexible output format. \n",
    "\n",
    "![Screenshot 2024-08-26 at 7.26.33 PM.png](https://cdn.prod.website-files.com/65b8cd72835ceeacd4449a53/66dbb164d61c93d48e604091_research-assistant1.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f23991e9-51b3-4e9f-86a0-dec16aa7d1e6",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%capture --no-stderr\n",
    "%pip install --quiet -U langgraph langchain_openai langchain_community langchain_core tavily-python wikipedia"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99a1c01d-87e1-4723-b83e-ebcf937fe914",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ba917800-10e4-4e2a-8e9e-30893b731e97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "OPENAI_API_KEY:  ········\n"
     ]
    }
   ],
   "source": [
    "import os, getpass\n",
    "\n",
    "def _set_env(var: str):\n",
    "    if not os.environ.get(var):\n",
    "        os.environ[var] = getpass.getpass(f\"{var}: \")\n",
    "\n",
    "_set_env(\"OPENAI_API_KEY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "afe9ff57-0826-4669-b88b-4d0501a509f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3419257b-2c6b-4d68-ae38-4a266cc02982",
   "metadata": {},
   "source": [
    "We'll use [LangSmith](https://docs.smith.langchain.com/) for [tracing](https://docs.smith.langchain.com/concepts/tracing)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5102cf2e-0ca9-465b-9499-67abb8132e5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "LANGCHAIN_API_KEY:  ········\n"
     ]
    }
   ],
   "source": [
    "_set_env(\"LANGCHAIN_API_KEY\")\n",
    "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
    "os.environ[\"LANGCHAIN_PROJECT\"] = \"langchain-academy\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8fe5d93-e353-44bb-be3e-434654bcb7ea",
   "metadata": {},
   "source": [
    "## Generate Analysts: Human-In-The-Loop\n",
    "\n",
    "Create analysts and review them using human-in-the-loop."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1eee8e60-e548-49b1-88ec-a4f3aef2174e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "from typing_extensions import TypedDict\n",
    "from pydantic import BaseModel, Field\n",
    "\n",
    "class Analyst(BaseModel):\n",
    "    affiliation: str = Field(\n",
    "        description=\"Primary affiliation of the analyst.\",\n",
    "    )\n",
    "    name: str = Field(\n",
    "        description=\"Name of the analyst.\"\n",
    "    )\n",
    "    role: str = Field(\n",
    "        description=\"Role of the analyst in the context of the topic.\",\n",
    "    )\n",
    "    description: str = Field(\n",
    "        description=\"Description of the analyst focus, concerns, and motives.\",\n",
    "    )\n",
    "    @property\n",
    "    def persona(self) -> str:\n",
    "        return f\"Name: {self.name}\\nRole: {self.role}\\nAffiliation: {self.affiliation}\\nDescription: {self.description}\\n\"\n",
    "\n",
    "class Perspectives(BaseModel):\n",
    "    analysts: List[Analyst] = Field(\n",
    "        description=\"Comprehensive list of analysts with their roles and affiliations.\",\n",
    "    )\n",
    "\n",
    "class GenerateAnalystsState(TypedDict):\n",
    "    topic: str # Research topic\n",
    "    max_analysts: int # Number of analysts\n",
    "    human_analyst_feedback: str # Human feedback\n",
    "    analysts: List[Analyst] # Analyst asking questions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fd088ff5-4c75-412c-85f0-04afd0900bfc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKkAAAF3CAIAAABR9PyTAAAAAXNSR0IArs4c6QAAIABJREFUeJztnXdYU1cDxk/2TiDsvRGcKO4BWveus9RVq9a6J+49qtZd9xYt4mrdWhUH7oWIirLClE0CZJP9/XH9KMUQQ5twQu79PT4+l3vOPfdN3px17xk4nU4HMFAJHrYADGhg3qMXzHv0gnmPXjDv0QvmPXohwhZgCLVKW5qnkIo0MrFaq9YpFQ2gO0qh4QkkHINFpLMITl5U2HIMgbPA/r2iUpMaL85KkhZkVDq4UxhsAp1F5DiQlHItbGlfh0zDlxcppWI1gYjLSZb5NGH4NmcEhLBg69KDxXn//IYg+6PUxZvm05ThGUSHLec/oVJosz5Icz5Kc1PlHQfaBbdlw1b0DyzI+/REcWx0cZte3Da9uLC1mBiZWP30qqCsRNl7jDPHngRbzmcsxftn1wSVMk3YUAcCEQdbi7moKFVeOVjQaZC9X3MmbC3AUrx/eo1PpuJb97C27K6XG8cKW4TZuPnTYAuxgD7ezRNFJDIOJcYDAPpNcHkTV570RAhbCGzv42PLOPakNr3s4MqoZwZMck2JFxdmyeHKgOl9TrJUKtJ06I8u4xGGz3Z/cbNMWQmz1wrT+4cX+C3COBAFwCWgJfPxJT5EAdC8//Bc6OZHs3EgwxIAnSbtOfkZ8opSJSwB0LzPeCvpNBiNpX11ugyxf/8YWqMPjvf5GXK1UkehEaDc3XLwCqa/fYgy77PeS32aMer5posWLbp69eq/uLBHjx4FBQVmUARwOJx3E3pWktQciX8VON4LChX1/2wrOTn5X1xVVFRUUVFhBjmfCWjJzM+QmS99A0B4rqfT6fbOy5ixw99M6V+6dCkmJiY/P59KpbZq1SoyMtLJyal169ZIKJPJjIuL02g0hw8fvnnzZklJCYfDCQ8Pnz17No1GQ4oHHA7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2W7gqqDAThQy/dK1rf16TddoVDdi9zOZZnzXrpQpnTzZ5kv/q8D0PiCE8el8GfAyyzPdgX1m06is67f3iMR8FtOucaMufXt+pb/OZNiMH7X50o3te49MtrVx6ddj2sNnZ5CGgjmoKJT5NHExU+LGAHnczv4FGY3CPc3R3LNwxKUytVg8ZLorRA2Qv/Tg9mxhEeTH2lCQlsmadYI8LBHyy9OOA+yOLs+ydau12jt0YlZu3ge9QVqNGk/Qrz9i6KqmwSabXnnv4Ynqz4WqQ6UwKxX6f7tTJ+x3cwnUGyQXKTSVCv8QQ52OegD+WM0nV/gFeTgHH/3DN0Qivlqj/8GnUqUg6+umAwCYDC6ZbLImpFwullfq7+ypVAq9jwoAAGyWA5GovwvzKbGw6zCuRyDk91jwvQcAnPo11zHQCSUjOETFEhpZ0XMU/MHaFtHIGjHbPeNZHmwV9YFcpBAXCS3BeEvxnkzFD5/jlveuELYQ86KUq/gZ/NGLPWEL+YxFeA8AsHel9hvvkPYwV62A+WrLfIj5sk9vCkct8oAt5G8sor6vQiZWn9qYa+djy3WH+cDL5AhyK/AaxZBpMHvzX2JZ3iPExpTkJMsc/LgcJwZsLf8VfnZFUVp5x0H2rbpZ3Dh0S/QeACAUqB78yS/KqmTa05kOdCaX2oCe/alVGnGpTMKX6dRq72B62FB72Ir0Y6HeI8jE6qwkaWqCVCJUS8tVZBqB7UCrlMCf0aIXEpkgLlco5WoHDxrLhhjYiuEdTDffoLT/jkV7Xx2lQisTqeUSjdZS24JEEqCxiAw2kUC00JVBatBgvMcwOZZbImGYG8x79IJ5j14w79EL5j16wbxHL/8D+eRQ+AdjN4gAAAAASUVORK5CYII=",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import Image, display\n",
    "from langgraph.graph import START, END, StateGraph\n",
    "from langgraph.checkpoint.memory import MemorySaver\n",
    "from langchain_core.messages import AIMessage, HumanMessage, SystemMessage\n",
    "\n",
    "analyst_instructions=\"\"\"You are tasked with creating a set of AI analyst personas. Follow these instructions carefully:\n",
    "\n",
    "1. First, review the research topic:\n",
    "{topic}\n",
    "        \n",
    "2. Examine any editorial feedback that has been optionally provided to guide creation of the analysts: \n",
    "        \n",
    "{human_analyst_feedback}\n",
    "    \n",
    "3. Determine the most interesting themes based upon documents and / or feedback above.\n",
    "                    \n",
    "4. Pick the top {max_analysts} themes.\n",
    "\n",
    "5. Assign one analyst to each theme.\"\"\"\n",
    "\n",
    "def create_analysts(state: GenerateAnalystsState):\n",
    "    \n",
    "    \"\"\" Create analysts \"\"\"\n",
    "    \n",
    "    topic=state['topic']\n",
    "    max_analysts=state['max_analysts']\n",
    "    human_analyst_feedback=state.get('human_analyst_feedback', '')\n",
    "        \n",
    "    # Enforce structured output\n",
    "    structured_llm = llm.with_structured_output(Perspectives)\n",
    "\n",
    "    # System message\n",
    "    system_message = analyst_instructions.format(topic=topic,\n",
    "                                                            human_analyst_feedback=human_analyst_feedback, \n",
    "                                                            max_analysts=max_analysts)\n",
    "\n",
    "    # Generate question \n",
    "    analysts = structured_llm.invoke([SystemMessage(content=system_message)]+[HumanMessage(content=\"Generate the set of analysts.\")])\n",
    "    \n",
    "    # Write the list of analysis to state\n",
    "    return {\"analysts\": analysts.analysts}\n",
    "\n",
    "def human_feedback(state: GenerateAnalystsState):\n",
    "    \"\"\" No-op node that should be interrupted on \"\"\"\n",
    "    pass\n",
    "\n",
    "def should_continue(state: GenerateAnalystsState):\n",
    "    \"\"\" Return the next node to execute \"\"\"\n",
    "\n",
    "    # Check if human feedback\n",
    "    human_analyst_feedback=state.get('human_analyst_feedback', None)\n",
    "    if human_analyst_feedback:\n",
    "        return \"create_analysts\"\n",
    "    \n",
    "    # Otherwise end\n",
    "    return END\n",
    "\n",
    "# Add nodes and edges \n",
    "builder = StateGraph(GenerateAnalystsState)\n",
    "builder.add_node(\"create_analysts\", create_analysts)\n",
    "builder.add_node(\"human_feedback\", human_feedback)\n",
    "builder.add_edge(START, \"create_analysts\")\n",
    "builder.add_edge(\"create_analysts\", \"human_feedback\")\n",
    "builder.add_conditional_edges(\"human_feedback\", should_continue, [\"create_analysts\", END])\n",
    "\n",
    "# Compile\n",
    "memory = MemorySaver()\n",
    "graph = builder.compile(interrupt_before=['human_feedback'], checkpointer=memory)\n",
    "\n",
    "# View\n",
    "display(Image(graph.get_graph(xray=1).draw_mermaid_png()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6c22cb05-c436-4358-8f7a-72d722f9b5cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Dr. Emily Carter\n",
      "Affiliation: Tech Innovators Inc.\n",
      "Role: Technology Adoption Specialist\n",
      "Description: Dr. Carter focuses on the strategic benefits of adopting new technologies like LangGraph. Her primary concern is how such frameworks can enhance operational efficiency and drive innovation within organizations. She is motivated by the potential for LangGraph to streamline processes and improve agent-based system performance.\n",
      "--------------------------------------------------\n",
      "Name: Mr. Raj Patel\n",
      "Affiliation: Data Security Solutions\n",
      "Role: Cybersecurity Analyst\n",
      "Description: Mr. Patel is dedicated to understanding the security implications of adopting new frameworks like LangGraph. His focus is on ensuring that the integration of such technologies does not compromise data integrity or security. He is motivated by the need to maintain robust security protocols while leveraging the benefits of advanced agent frameworks.\n",
      "--------------------------------------------------\n",
      "Name: Ms. Sarah Nguyen\n",
      "Affiliation: GreenTech Ventures\n",
      "Role: Sustainability Consultant\n",
      "Description: Ms. Nguyen explores the environmental impact and sustainability benefits of adopting technologies like LangGraph. Her concern is how these frameworks can contribute to reducing carbon footprints and promoting sustainable practices in tech development. She is driven by the potential for LangGraph to support eco-friendly initiatives and sustainable growth.\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# Input\n",
    "max_analysts = 3 \n",
    "topic = \"The benefits of adopting LangGraph as an agent framework\"\n",
    "thread = {\"configurable\": {\"thread_id\": \"1\"}}\n",
    "\n",
    "# Run the graph until the first interruption\n",
    "for event in graph.stream({\"topic\":topic,\"max_analysts\":max_analysts,}, thread, stream_mode=\"values\"):\n",
    "    # Review\n",
    "    analysts = event.get('analysts', '')\n",
    "    if analysts:\n",
    "        for analyst in analysts:\n",
    "            print(f\"Name: {analyst.name}\")\n",
    "            print(f\"Affiliation: {analyst.affiliation}\")\n",
    "            print(f\"Role: {analyst.role}\")\n",
    "            print(f\"Description: {analyst.description}\")\n",
    "            print(\"-\" * 50)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2f81ad23-5656-43e6-b50a-0d7a4f69a60a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('human_feedback',)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get state and look at next node\n",
    "state = graph.get_state(thread)\n",
    "state.next"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "72b2a402-fd10-4f26-9a32-3e3c0d4aaf76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'configurable': {'thread_id': '1',\n",
       "  'checkpoint_ns': '',\n",
       "  'checkpoint_id': '1effd5ef-c39e-6210-8002-b7beccc86633'}}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We now update the state as if we are the human_feedback node\n",
    "graph.update_state(thread, {\"human_analyst_feedback\": \n",
    "                            \"Add in someone from a startup to add an entrepreneur perspective\"}, as_node=\"human_feedback\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b8816eb9-9906-441b-b552-be71107db14f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Dr. Emily Carter\n",
      "Affiliation: Tech Innovators Inc.\n",
      "Role: Technology Adoption Specialist\n",
      "Description: Dr. Carter focuses on the strategic benefits of adopting new technologies like LangGraph. Her primary concern is how such frameworks can enhance operational efficiency and drive innovation within organizations. She is motivated by the potential for LangGraph to streamline processes and improve agent-based system performance.\n",
      "--------------------------------------------------\n",
      "Name: Mr. Raj Patel\n",
      "Affiliation: Data Security Solutions\n",
      "Role: Cybersecurity Analyst\n",
      "Description: Mr. Patel is dedicated to understanding the security implications of adopting new frameworks like LangGraph. His focus is on ensuring that the integration of such technologies does not compromise data integrity or security. He is motivated by the need to maintain robust security protocols while leveraging the benefits of advanced agent frameworks.\n",
      "--------------------------------------------------\n",
      "Name: Ms. Sarah Nguyen\n",
      "Affiliation: GreenTech Ventures\n",
      "Role: Sustainability Consultant\n",
      "Description: Ms. Nguyen explores the environmental impact and sustainability benefits of adopting technologies like LangGraph. Her concern is how these frameworks can contribute to reducing carbon footprints and promoting sustainable practices in tech development. She is driven by the potential for LangGraph to support eco-friendly initiatives and sustainable growth.\n",
      "--------------------------------------------------\n",
      "Name: Alice Johnson\n",
      "Affiliation: Tech Innovators Inc.\n",
      "Role: Startup Entrepreneur\n",
      "Description: Alice is a co-founder of a tech startup that focuses on developing innovative software solutions. Her interest in LangGraph stems from its potential to streamline development processes and reduce time-to-market for new applications. She is particularly focused on how adopting LangGraph can provide a competitive edge and foster rapid innovation within her company.\n",
      "--------------------------------------------------\n",
      "Name: Dr. Michael Lee\n",
      "Affiliation: Global Tech Solutions\n",
      "Role: Senior Software Architect\n",
      "Description: Dr. Lee is a seasoned software architect with over 20 years of experience in designing scalable systems. His focus is on the technical benefits of LangGraph, such as its ability to enhance system interoperability and improve the efficiency of agent-based architectures. He is interested in how LangGraph can be integrated into existing systems to optimize performance and scalability.\n",
      "--------------------------------------------------\n",
      "Name: Professor Emily Carter\n",
      "Affiliation: University of Technology\n",
      "Role: Academic Researcher\n",
      "Description: Professor Carter is a leading researcher in the field of artificial intelligence and agent-based systems. Her research explores the theoretical underpinnings of frameworks like LangGraph and their implications for the future of AI development. She is particularly interested in the educational benefits of LangGraph, such as its potential to serve as a teaching tool for students learning about agent-based modeling and AI.\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# Continue the graph execution\n",
    "for event in graph.stream(None, thread, stream_mode=\"values\"):\n",
    "    # Review\n",
    "    analysts = event.get('analysts', '')\n",
    "    if analysts:\n",
    "        for analyst in analysts:\n",
    "            print(f\"Name: {analyst.name}\")\n",
    "            print(f\"Affiliation: {analyst.affiliation}\")\n",
    "            print(f\"Role: {analyst.role}\")\n",
    "            print(f\"Description: {analyst.description}\")\n",
    "            print(\"-\" * 50) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a43ac322-5926-4932-8653-68206fec0d2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'configurable': {'thread_id': '1',\n",
       "  'checkpoint_ns': '',\n",
       "  'checkpoint_id': '1effd5ef-fa50-69ab-8004-6d2cbd9095b2'}}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# If we are satisfied, then we simply supply no feedback\n",
    "further_feedack = None\n",
    "graph.update_state(thread, {\"human_analyst_feedback\": \n",
    "                            further_feedack}, as_node=\"human_feedback\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ab034e65-aeee-4723-8d6d-74541b548425",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Continue the graph execution to end\n",
    "for event in graph.stream(None, thread, stream_mode=\"updates\"):\n",
    "    print(\"--Node--\")\n",
    "    node_name = next(iter(event.keys()))\n",
    "    print(node_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2f204e8a-285c-4e46-8223-a695caec7764",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_state = graph.get_state(thread)\n",
    "analysts = final_state.values.get('analysts')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "59704086-cb3b-42e9-8395-37be6f0d44e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_state.next"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "95717ba3-aa00-48d6-bbb7-5fe4db5919bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Alice Johnson\n",
      "Affiliation: Tech Innovators Inc.\n",
      "Role: Startup Entrepreneur\n",
      "Description: Alice is a co-founder of a tech startup that focuses on developing innovative software solutions. Her interest in LangGraph stems from its potential to streamline development processes and reduce time-to-market for new applications. She is particularly focused on how adopting LangGraph can provide a competitive edge and foster rapid innovation within her company.\n",
      "--------------------------------------------------\n",
      "Name: Dr. Michael Lee\n",
      "Affiliation: Global Tech Solutions\n",
      "Role: Senior Software Architect\n",
      "Description: Dr. Lee is a seasoned software architect with over 20 years of experience in designing scalable systems. His focus is on the technical benefits of LangGraph, such as its ability to enhance system interoperability and improve the efficiency of agent-based architectures. He is interested in how LangGraph can be integrated into existing systems to optimize performance and scalability.\n",
      "--------------------------------------------------\n",
      "Name: Professor Emily Carter\n",
      "Affiliation: University of Technology\n",
      "Role: Academic Researcher\n",
      "Description: Professor Carter is a leading researcher in the field of artificial intelligence and agent-based systems. Her research explores the theoretical underpinnings of frameworks like LangGraph and their implications for the future of AI development. She is particularly interested in the educational benefits of LangGraph, such as its potential to serve as a teaching tool for students learning about agent-based modeling and AI.\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "for analyst in analysts:\n",
    "    print(f\"Name: {analyst.name}\")\n",
    "    print(f\"Affiliation: {analyst.affiliation}\")\n",
    "    print(f\"Role: {analyst.role}\")\n",
    "    print(f\"Description: {analyst.description}\")\n",
    "    print(\"-\" * 50) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d2498e4-20ae-4503-9dd0-a4165132b7a7",
   "metadata": {},
   "source": [
    "## Conduct Interview\n",
    "\n",
    "### Generate Question\n",
    "\n",
    "The analyst will ask questions to the expert."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e5d5f559-f42e-442b-87cd-dbf0a91abf9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import operator\n",
    "from typing import  Annotated\n",
    "from langgraph.graph import MessagesState\n",
    "\n",
    "class InterviewState(MessagesState):\n",
    "    max_num_turns: int # Number turns of conversation\n",
    "    context: Annotated[list, operator.add] # Source docs\n",
    "    analyst: Analyst # Analyst asking questions\n",
    "    interview: str # Interview transcript\n",
    "    sections: list # Final key we duplicate in outer state for Send() API\n",
    "\n",
    "class SearchQuery(BaseModel):\n",
    "    search_query: str = Field(None, description=\"Search query for retrieval.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1c2e71eb-07ad-4bea-aabc-dbaf551408c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "question_instructions = \"\"\"You are an analyst tasked with interviewing an expert to learn about a specific topic. \n",
    "\n",
    "Your goal is boil down to interesting and specific insights related to your topic.\n",
    "\n",
    "1. Interesting: Insights that people will find surprising or non-obvious.\n",
    "        \n",
    "2. Specific: Insights that avoid generalities and include specific examples from the expert.\n",
    "\n",
    "Here is your topic of focus and set of goals: {goals}\n",
    "        \n",
    "Begin by introducing yourself using a name that fits your persona, and then ask your question.\n",
    "\n",
    "Continue to ask questions to drill down and refine your understanding of the topic.\n",
    "        \n",
    "When you are satisfied with your understanding, complete the interview with: \"Thank you so much for your help!\"\n",
    "\n",
    "Remember to stay in character throughout your response, reflecting the persona and goals provided to you.\"\"\"\n",
    "\n",
    "def generate_question(state: InterviewState):\n",
    "    \"\"\" Node to generate a question \"\"\"\n",
    "\n",
    "    # Get state\n",
    "    analyst = state[\"analyst\"]\n",
    "    messages = state[\"messages\"]\n",
    "\n",
    "    # Generate question \n",
    "    system_message = question_instructions.format(goals=analyst.persona)\n",
    "    question = llm.invoke([SystemMessage(content=system_message)]+messages)\n",
    "        \n",
    "    # Write messages to state\n",
    "    return {\"messages\": [question]}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be2ff33a-6232-4a79-8a82-882a645394f5",
   "metadata": {},
   "source": [
    "### Generate Answer: Parallelization\n",
    "\n",
    "The expert will gather information from multiple sources in parallel to answer questions.\n",
    "\n",
    "For example, we can use:\n",
    "\n",
    "* Specific web sites e.g., via [`WebBaseLoader`](https://python.langchain.com/v0.2/docs/integrations/document_loaders/web_base/)\n",
    "* Indexed documents e.g., via [RAG](https://python.langchain.com/v0.2/docs/tutorials/rag/)\n",
    "* Web search\n",
    "* Wikipedia search\n",
    "\n",
    "You can try different web search tools, like [Tavily](https://tavily.com/)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "606ea95b-e811-4299-8b66-835d4016c338",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "TAVILY_API_KEY:  ········\n"
     ]
    }
   ],
   "source": [
    "def _set_env(var: str):\n",
    "    if not os.environ.get(var):\n",
    "        os.environ[var] = getpass.getpass(f\"{var}: \")\n",
    "\n",
    "_set_env(\"TAVILY_API_KEY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c61ae74a-f838-4e97-8bd5-48ccd15b7789",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Web search tool\n",
    "from langchain_community.tools.tavily_search import TavilySearchResults\n",
    "tavily_search = TavilySearchResults(max_results=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2d8f760b-5a1a-4fa9-a014-d3fb02bec51c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Wikipedia search tool\n",
    "from langchain_community.document_loaders import WikipediaLoader"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06cb1603",
   "metadata": {},
   "source": [
    "Now, we create nodes to search the web and wikipedia.\n",
    "\n",
    "We'll also create a node to answer analyst questions.\n",
    "\n",
    "Finally, we'll create nodes to save the full interview and to write a summary (\"section\") of the interview."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9c863768-2278-415b-aef1-96fd18c1b1cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.messages import get_buffer_string\n",
    "\n",
    "# Search query writing\n",
    "search_instructions = SystemMessage(content=f\"\"\"You will be given a conversation between an analyst and an expert. \n",
    "\n",
    "Your goal is to generate a well-structured query for use in retrieval and / or web-search related to the conversation.\n",
    "        \n",
    "First, analyze the full conversation.\n",
    "\n",
    "Pay particular attention to the final question posed by the analyst.\n",
    "\n",
    "Convert this final question into a well-structured web search query\"\"\")\n",
    "\n",
    "def search_web(state: InterviewState):\n",
    "    \n",
    "    \"\"\" Retrieve docs from web search \"\"\"\n",
    "\n",
    "    # Search query\n",
    "    structured_llm = llm.with_structured_output(SearchQuery)\n",
    "    search_query = structured_llm.invoke([search_instructions]+state['messages'])\n",
    "    \n",
    "    # Search\n",
    "    search_docs = tavily_search.invoke(search_query.search_query)\n",
    "\n",
    "     # Format\n",
    "    formatted_search_docs = \"\\n\\n---\\n\\n\".join(\n",
    "        [\n",
    "            f'<Document href=\"{doc[\"url\"]}\"/>\\n{doc[\"content\"]}\\n</Document>'\n",
    "            for doc in search_docs\n",
    "        ]\n",
    "    )\n",
    "\n",
    "    return {\"context\": [formatted_search_docs]} \n",
    "\n",
    "def search_wikipedia(state: InterviewState):\n",
    "    \n",
    "    \"\"\" Retrieve docs from wikipedia \"\"\"\n",
    "\n",
    "    # Search query\n",
    "    structured_llm = llm.with_structured_output(SearchQuery)\n",
    "    search_query = structured_llm.invoke([search_instructions]+state['messages'])\n",
    "    \n",
    "    # Search\n",
    "    search_docs = WikipediaLoader(query=search_query.search_query, \n",
    "                                  load_max_docs=2).load()\n",
    "\n",
    "     # Format\n",
    "    formatted_search_docs = \"\\n\\n---\\n\\n\".join(\n",
    "        [\n",
    "            f'<Document source=\"{doc.metadata[\"source\"]}\" page=\"{doc.metadata.get(\"page\", \"\")}\"/>\\n{doc.page_content}\\n</Document>'\n",
    "            for doc in search_docs\n",
    "        ]\n",
    "    )\n",
    "\n",
    "    return {\"context\": [formatted_search_docs]} \n",
    "\n",
    "answer_instructions = \"\"\"You are an expert being interviewed by an analyst.\n",
    "\n",
    "Here is analyst area of focus: {goals}. \n",
    "        \n",
    "You goal is to answer a question posed by the interviewer.\n",
    "\n",
    "To answer question, use this context:\n",
    "        \n",
    "{context}\n",
    "\n",
    "When answering questions, follow these guidelines:\n",
    "        \n",
    "1. Use only the information provided in the context. \n",
    "        \n",
    "2. Do not introduce external information or make assumptions beyond what is explicitly stated in the context.\n",
    "\n",
    "3. The context contain sources at the topic of each individual document.\n",
    "\n",
    "4. Include these sources your answer next to any relevant statements. For example, for source # 1 use [1]. \n",
    "\n",
    "5. List your sources in order at the bottom of your answer. [1] Source 1, [2] Source 2, etc\n",
    "        \n",
    "6. If the source is: <Document source=\"assistant/docs/llama3_1.pdf\" page=\"7\"/>' then just list: \n",
    "        \n",
    "[1] assistant/docs/llama3_1.pdf, page 7 \n",
    "        \n",
    "And skip the addition of the brackets as well as the Document source preamble in your citation.\"\"\"\n",
    "\n",
    "def generate_answer(state: InterviewState):\n",
    "    \n",
    "    \"\"\" Node to answer a question \"\"\"\n",
    "\n",
    "    # Get state\n",
    "    analyst = state[\"analyst\"]\n",
    "    messages = state[\"messages\"]\n",
    "    context = state[\"context\"]\n",
    "\n",
    "    # Answer question\n",
    "    system_message = answer_instructions.format(goals=analyst.persona, context=context)\n",
    "    answer = llm.invoke([SystemMessage(content=system_message)]+messages)\n",
    "            \n",
    "    # Name the message as coming from the expert\n",
    "    answer.name = \"expert\"\n",
    "    \n",
    "    # Append it to state\n",
    "    return {\"messages\": [answer]}\n",
    "\n",
    "def save_interview(state: InterviewState):\n",
    "    \n",
    "    \"\"\" Save interviews \"\"\"\n",
    "\n",
    "    # Get messages\n",
    "    messages = state[\"messages\"]\n",
    "    \n",
    "    # Convert interview to a string\n",
    "    interview = get_buffer_string(messages)\n",
    "    \n",
    "    # Save to interviews key\n",
    "    return {\"interview\": interview}\n",
    "\n",
    "def route_messages(state: InterviewState, \n",
    "                   name: str = \"expert\"):\n",
    "\n",
    "    \"\"\" Route between question and answer \"\"\"\n",
    "    \n",
    "    # Get messages\n",
    "    messages = state[\"messages\"]\n",
    "    max_num_turns = state.get('max_num_turns',2)\n",
    "\n",
    "    # Check the number of expert answers \n",
    "    num_responses = len(\n",
    "        [m for m in messages if isinstance(m, AIMessage) and m.name == name]\n",
    "    )\n",
    "\n",
    "    # End if expert has answered more than the max turns\n",
    "    if num_responses >= max_num_turns:\n",
    "        return 'save_interview'\n",
    "\n",
    "    # This router is run after each question - answer pair \n",
    "    # Get the last question asked to check if it signals the end of discussion\n",
    "    last_question = messages[-2]\n",
    "    \n",
    "    if \"Thank you so much for your help\" in last_question.content:\n",
    "        return 'save_interview'\n",
    "    return \"ask_question\"\n",
    "\n",
    "section_writer_instructions = \"\"\"You are an expert technical writer. \n",
    "            \n",
    "Your task is to create a short, easily digestible section of a report based on a set of source documents.\n",
    "\n",
    "1. Analyze the content of the source documents: \n",
    "- The name of each source document is at the start of the document, with the <Document tag.\n",
    "        \n",
    "2. Create a report structure using markdown formatting:\n",
    "- Use ## for the section title\n",
    "- Use ### for sub-section headers\n",
    "        \n",
    "3. Write the report following this structure:\n",
    "a. Title (## header)\n",
    "b. Summary (### header)\n",
    "c. Sources (### header)\n",
    "\n",
    "4. Make your title engaging based upon the focus area of the analyst: \n",
    "{focus}\n",
    "\n",
    "5. For the summary section:\n",
    "- Set up summary with general background / context related to the focus area of the analyst\n",
    "- Emphasize what is novel, interesting, or surprising about insights gathered from the interview\n",
    "- Create a numbered list of source documents, as you use them\n",
    "- Do not mention the names of interviewers or experts\n",
    "- Aim for approximately 400 words maximum\n",
    "- Use numbered sources in your report (e.g., [1], [2]) based on information from source documents\n",
    "        \n",
    "6. In the Sources section:\n",
    "- Include all sources used in your report\n",
    "- Provide full links to relevant websites or specific document paths\n",
    "- Separate each source by a newline. Use two spaces at the end of each line to create a newline in Markdown.\n",
    "- It will look like:\n",
    "\n",
    "### Sources\n",
    "[1] Link or Document name\n",
    "[2] Link or Document name\n",
    "\n",
    "7. Be sure to combine sources. For example this is not correct:\n",
    "\n",
    "[3] https://ai.meta.com/blog/meta-llama-3-1/\n",
    "[4] https://ai.meta.com/blog/meta-llama-3-1/\n",
    "\n",
    "There should be no redundant sources. It should simply be:\n",
    "\n",
    "[3] https://ai.meta.com/blog/meta-llama-3-1/\n",
    "        \n",
    "8. Final review:\n",
    "- Ensure the report follows the required structure\n",
    "- Include no preamble before the title of the report\n",
    "- Check that all guidelines have been followed\"\"\"\n",
    "\n",
    "def write_section(state: InterviewState):\n",
    "\n",
    "    \"\"\" Node to answer a question \"\"\"\n",
    "\n",
    "    # Get state\n",
    "    interview = state[\"interview\"]\n",
    "    context = state[\"context\"]\n",
    "    analyst = state[\"analyst\"]\n",
    "   \n",
    "    # Write section using either the gathered source docs from interview (context) or the interview itself (interview)\n",
    "    system_message = section_writer_instructions.format(focus=analyst.description)\n",
    "    section = llm.invoke([SystemMessage(content=system_message)]+[HumanMessage(content=f\"Use this source to write your section: {context}\")]) \n",
    "                \n",
    "    # Append it to state\n",
    "    return {\"sections\": [section.content]}\n",
    "\n",
    "# Add nodes and edges \n",
    "interview_builder = StateGraph(InterviewState)\n",
    "interview_builder.add_node(\"ask_question\", generate_question)\n",
    "interview_builder.add_node(\"search_web\", search_web)\n",
    "interview_builder.add_node(\"search_wikipedia\", search_wikipedia)\n",
    "interview_builder.add_node(\"answer_question\", generate_answer)\n",
    "interview_builder.add_node(\"save_interview\", save_interview)\n",
    "interview_builder.add_node(\"write_section\", write_section)\n",
    "\n",
    "# Flow\n",
    "interview_builder.add_edge(START, \"ask_question\")\n",
    "interview_builder.add_edge(\"ask_question\", \"search_web\")\n",
    "interview_builder.add_edge(\"ask_question\", \"search_wikipedia\")\n",
    "interview_builder.add_edge(\"search_web\", \"answer_question\")\n",
    "interview_builder.add_edge(\"search_wikipedia\", \"answer_question\")\n",
    "interview_builder.add_conditional_edges(\"answer_question\", route_messages,['ask_question','save_interview'])\n",
    "interview_builder.add_edge(\"save_interview\", \"write_section\")\n",
    "interview_builder.add_edge(\"write_section\", END)\n",
    "\n",
    "# Interview \n",
    "memory = MemorySaver()\n",
    "interview_graph = interview_builder.compile(checkpointer=memory).with_config(run_name=\"Conduct Interviews\")\n",
    "\n",
    "# View\n",
    "#display(Image(interview_graph.get_graph().draw_mermaid_png()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "50f382f1-6e93-48d0-a44a-1094d26ccb1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Analyst(affiliation='Tech Innovators Inc.', name='Alice Johnson', role='Startup Entrepreneur', description='Alice is a co-founder of a tech startup that focuses on developing innovative software solutions. Her interest in LangGraph stems from its potential to streamline development processes and reduce time-to-market for new applications. She is particularly focused on how adopting LangGraph can provide a competitive edge and foster rapid innovation within her company.')"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Pick one analyst\n",
    "analysts[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3750ac4f-f458-4b2d-8bad-32ce34895758",
   "metadata": {},
   "source": [
    "Here, we run the interview passing an index of the llama3.1 paper, which is related to our topic."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b2242d4e-8430-4de9-8cf7-3ad2f9a22b28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "## Leveraging LangGraph for Competitive Advantage in Software Development\n",
       "\n",
       "### Summary\n",
       "\n",
       "In the rapidly evolving tech landscape, startups like Alice's are constantly seeking innovative solutions to streamline development processes and accelerate time-to-market. LangGraph, a novel AI library, presents a promising opportunity for companies aiming to gain a competitive edge through advanced software solutions. LangGraph's unique capabilities in building stateful, multi-actor applications with Large Language Models (LLMs) offer significant advantages for startups focused on rapid innovation and efficiency.\n",
       "\n",
       "LangGraph distinguishes itself by utilizing cyclic data flows and high-level abstraction, which are particularly beneficial for developing complex, multi-step conversational AI applications. This framework allows for the creation of advanced chatbots and interactive AI systems that require flexible and customized interactions, making it a robust choice for startups aiming to enhance their software offerings [1]. The ability to manage sophisticated language tasks through cyclical graphs enables more adaptable and customizable agent runtimes, which is crucial for maintaining a competitive edge in the tech industry [2].\n",
       "\n",
       "One of the most compelling aspects of LangGraph is its integration with LangChain, which simplifies the deployment and operation of agent-based systems. This integration allows developers to build graph-based agents that can efficiently transition between nodes, such as agents and tools, based on specific conditions. This capability is essential for creating robust frameworks that support complex conversational AI applications, thereby reducing development time and improving the overall efficiency of software solutions [3].\n",
       "\n",
       "Furthermore, LangGraph's approach to agent creation and tool integration is noteworthy. By defining sub-agents with specific tool access, such as financial data retrieval or web research, LangGraph facilitates the orchestration of tasks and enhances the overall functionality of AI systems. This modular approach not only streamlines the development process but also allows for more efficient task delegation and execution, which is vital for startups looking to innovate quickly and effectively [4].\n",
       "\n",
       "In summary, LangGraph offers a powerful framework for startups like Alice's to develop innovative software solutions that can significantly reduce time-to-market and provide a competitive advantage. Its ability to handle complex language tasks, coupled with its integration with LangChain, makes it an attractive option for companies focused on rapid innovation and efficiency in the tech industry.\n",
       "\n",
       "### Sources\n",
       "[1] https://www.linkedin.com/pulse/what-langgraph-how-useful-building-llm-based-sarfraz-nawaz-walic  \n",
       "[2] https://www.reddit.com/r/LangChain/comments/1g1pkki/openais_new_framework_for_agents_why_is_langgraph/  \n",
       "[3] https://medium.com/@shravankoninti/langgraph-and-agents-application-3134ef650998  \n",
       "[4] https://www.relari.ai/blog/ai-agent-framework-comparison-langgraph-crewai-openai-swarm  "
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import Markdown\n",
    "messages = [HumanMessage(f\"So you said you were writing an article on {topic}?\")]\n",
    "thread = {\"configurable\": {\"thread_id\": \"1\"}}\n",
    "interview = interview_graph.invoke({\"analyst\": analysts[0], \"messages\": messages, \"max_num_turns\": 2}, thread)\n",
    "Markdown(interview['sections'][0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b739e87-68bb-4e96-a86a-704e84240a6c",
   "metadata": {},
   "source": [
    "### Parallelze interviews: Map-Reduce\n",
    "\n",
    "We parallelize the interviews via the `Send()` API, a map step.\n",
    "\n",
    "We combine them into the report body in a reduce step.\n",
    "\n",
    "### Finalize\n",
    "\n",
    "We add a final step to write an intro and conclusion to the final report."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "6a0042f9-5b9f-441a-9e8d-7d8189f44140",
   "metadata": {},
   "outputs": [],
   "source": [
    "import operator\n",
    "from typing import List, Annotated\n",
    "from typing_extensions import TypedDict\n",
    "\n",
    "class ResearchGraphState(TypedDict):\n",
    "    topic: str # Research topic\n",
    "    max_analysts: int # Number of analysts\n",
    "    human_analyst_feedback: str # Human feedback\n",
    "    analysts: List[Analyst] # Analyst asking questions\n",
    "    sections: Annotated[list, operator.add] # Send() API key\n",
    "    introduction: str # Introduction for the final report\n",
    "    content: str # Content for the final report\n",
    "    conclusion: str # Conclusion for the final report\n",
    "    final_report: str # Final report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c2224592-d2ff-469d-97bd-928809f896d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langgraph.constants import Send\n",
    "\n",
    "def initiate_all_interviews(state: ResearchGraphState):\n",
    "    \"\"\" This is the \"map\" step where we run each interview sub-graph using Send API \"\"\"    \n",
    "\n",
    "    # Check if human feedback\n",
    "    human_analyst_feedback=state.get('human_analyst_feedback')\n",
    "    if human_analyst_feedback:\n",
    "        # Return to create_analysts\n",
    "        return \"create_analysts\"\n",
    "\n",
    "    # Otherwise kick off interviews in parallel via Send() API\n",
    "    else:\n",
    "        topic = state[\"topic\"]\n",
    "        return [Send(\"conduct_interview\", {\"analyst\": analyst,\n",
    "                                           \"messages\": [HumanMessage(\n",
    "                                               content=f\"So you said you were writing an article on {topic}?\"\n",
    "                                           )\n",
    "                                                       ]}) for analyst in state[\"analysts\"]]\n",
    "\n",
    "report_writer_instructions = \"\"\"You are a technical writer creating a report on this overall topic: \n",
    "\n",
    "{topic}\n",
    "    \n",
    "You have a team of analysts. Each analyst has done two things: \n",
    "\n",
    "1. They conducted an interview with an expert on a specific sub-topic.\n",
    "2. They write up their finding into a memo.\n",
    "\n",
    "Your task: \n",
    "\n",
    "1. You will be given a collection of memos from your analysts.\n",
    "2. Think carefully about the insights from each memo.\n",
    "3. Consolidate these into a crisp overall summary that ties together the central ideas from all of the memos. \n",
    "4. Summarize the central points in each memo into a cohesive single narrative.\n",
    "\n",
    "To format your report:\n",
    " \n",
    "1. Use markdown formatting. \n",
    "2. Include no pre-amble for the report.\n",
    "3. Use no sub-heading. \n",
    "4. Start your report with a single title header: ## Insights\n",
    "5. Do not mention any analyst names in your report.\n",
    "6. Preserve any citations in the memos, which will be annotated in brackets, for example [1] or [2].\n",
    "7. Create a final, consolidated list of sources and add to a Sources section with the `## Sources` header.\n",
    "8. List your sources in order and do not repeat.\n",
    "\n",
    "[1] Source 1\n",
    "[2] Source 2\n",
    "\n",
    "Here are the memos from your analysts to build your report from: \n",
    "\n",
    "{context}\"\"\"\n",
    "\n",
    "def write_report(state: ResearchGraphState):\n",
    "    # Full set of sections\n",
    "    sections = state[\"sections\"]\n",
    "    topic = state[\"topic\"]\n",
    "\n",
    "    # Concat all sections together\n",
    "    formatted_str_sections = \"\\n\\n\".join([f\"{section}\" for section in sections])\n",
    "    \n",
    "    # Summarize the sections into a final report\n",
    "    system_message = report_writer_instructions.format(topic=topic, context=formatted_str_sections)    \n",
    "    report = llm.invoke([SystemMessage(content=system_message)]+[HumanMessage(content=f\"Write a report based upon these memos.\")]) \n",
    "    return {\"content\": report.content}\n",
    "\n",
    "intro_conclusion_instructions = \"\"\"You are a technical writer finishing a report on {topic}\n",
    "\n",
    "You will be given all of the sections of the report.\n",
    "\n",
    "You job is to write a crisp and compelling introduction or conclusion section.\n",
    "\n",
    "The user will instruct you whether to write the introduction or conclusion.\n",
    "\n",
    "Include no pre-amble for either section.\n",
    "\n",
    "Target around 100 words, crisply previewing (for introduction) or recapping (for conclusion) all of the sections of the report.\n",
    "\n",
    "Use markdown formatting. \n",
    "\n",
    "For your introduction, create a compelling title and use the # header for the title.\n",
    "\n",
    "For your introduction, use ## Introduction as the section header. \n",
    "\n",
    "For your conclusion, use ## Conclusion as the section header.\n",
    "\n",
    "Here are the sections to reflect on for writing: {formatted_str_sections}\"\"\"\n",
    "\n",
    "def write_introduction(state: ResearchGraphState):\n",
    "    # Full set of sections\n",
    "    sections = state[\"sections\"]\n",
    "    topic = state[\"topic\"]\n",
    "\n",
    "    # Concat all sections together\n",
    "    formatted_str_sections = \"\\n\\n\".join([f\"{section}\" for section in sections])\n",
    "    \n",
    "    # Summarize the sections into a final report\n",
    "    \n",
    "    instructions = intro_conclusion_instructions.format(topic=topic, formatted_str_sections=formatted_str_sections)    \n",
    "    intro = llm.invoke([instructions]+[HumanMessage(content=f\"Write the report introduction\")]) \n",
    "    return {\"introduction\": intro.content}\n",
    "\n",
    "def write_conclusion(state: ResearchGraphState):\n",
    "    # Full set of sections\n",
    "    sections = state[\"sections\"]\n",
    "    topic = state[\"topic\"]\n",
    "\n",
    "    # Concat all sections together\n",
    "    formatted_str_sections = \"\\n\\n\".join([f\"{section}\" for section in sections])\n",
    "    \n",
    "    # Summarize the sections into a final report\n",
    "    \n",
    "    instructions = intro_conclusion_instructions.format(topic=topic, formatted_str_sections=formatted_str_sections)    \n",
    "    conclusion = llm.invoke([instructions]+[HumanMessage(content=f\"Write the report conclusion\")]) \n",
    "    return {\"conclusion\": conclusion.content}\n",
    "\n",
    "def finalize_report(state: ResearchGraphState):\n",
    "    \"\"\" The is the \"reduce\" step where we gather all the sections, combine them, and reflect on them to write the intro/conclusion \"\"\"\n",
    "    # Save full final report\n",
    "    content = state[\"content\"]\n",
    "    if content.startswith(\"## Insights\"):\n",
    "        content = content.strip(\"## Insights\")\n",
    "    if \"## Sources\" in content:\n",
    "        try:\n",
    "            content, sources = content.split(\"\\n## Sources\\n\")\n",
    "        except:\n",
    "            sources = None\n",
    "    else:\n",
    "        sources = None\n",
    "\n",
    "    final_report = state[\"introduction\"] + \"\\n\\n---\\n\\n\" + content + \"\\n\\n---\\n\\n\" + state[\"conclusion\"]\n",
    "    if sources is not None:\n",
    "        final_report += \"\\n\\n## Sources\\n\" + sources\n",
    "    return {\"final_report\": final_report}\n",
    "\n",
    "# Add nodes and edges \n",
    "builder = StateGraph(ResearchGraphState)\n",
    "builder.add_node(\"create_analysts\", create_analysts)\n",
    "builder.add_node(\"human_feedback\", human_feedback)\n",
    "builder.add_node(\"conduct_interview\", interview_builder.compile())\n",
    "builder.add_node(\"write_report\",write_report)\n",
    "builder.add_node(\"write_introduction\",write_introduction)\n",
    "builder.add_node(\"write_conclusion\",write_conclusion)\n",
    "builder.add_node(\"finalize_report\",finalize_report)\n",
    "\n",
    "# Logic\n",
    "builder.add_edge(START, \"create_analysts\")\n",
    "builder.add_edge(\"create_analysts\", \"human_feedback\")\n",
    "builder.add_conditional_edges(\"human_feedback\", initiate_all_interviews, [\"create_analysts\", \"conduct_interview\"])\n",
    "builder.add_edge(\"conduct_interview\", \"write_report\")\n",
    "builder.add_edge(\"conduct_interview\", \"write_introduction\")\n",
    "builder.add_edge(\"conduct_interview\", \"write_conclusion\")\n",
    "builder.add_edge([\"write_conclusion\", \"write_report\", \"write_introduction\"], \"finalize_report\")\n",
    "builder.add_edge(\"finalize_report\", END)\n",
    "\n",
    "# Compile\n",
    "memory = MemorySaver()\n",
    "graph = builder.compile(interrupt_before=['human_feedback'], checkpointer=memory)\n",
    "# display(Image(graph.get_graph(xray=1).draw_mermaid_png()))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b64ba9a-2b5e-40e1-a778-0f635aa3f6d0",
   "metadata": {},
   "source": [
    "Let's ask an open-ended question about LangGraph."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "362932ee-4106-4a2d-a32d-b812eafcf9df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Dr. Emily Carter\n",
      "Affiliation: Tech Innovators Inc.\n",
      "Role: Technology Adoption Specialist\n",
      "Description: Dr. Carter focuses on the strategic benefits of adopting new technologies like LangGraph. She is particularly interested in how LangGraph can streamline processes, improve efficiency, and provide a competitive edge to organizations. Her analysis often includes case studies and data-driven insights to support the adoption of innovative frameworks.\n",
      "--------------------------------------------------\n",
      "Name: Mr. Raj Patel\n",
      "Affiliation: Data Security Solutions\n",
      "Role: Cybersecurity Analyst\n",
      "Description: Mr. Patel is concerned with the security implications of adopting new frameworks such as LangGraph. His focus is on understanding how LangGraph can enhance or compromise data security within organizations. He evaluates the framework's security features, potential vulnerabilities, and compliance with industry standards.\n",
      "--------------------------------------------------\n",
      "Name: Ms. Sarah Lee\n",
      "Affiliation: GreenTech Ventures\n",
      "Role: Sustainability Consultant\n",
      "Description: Ms. Lee examines the environmental impact and sustainability benefits of adopting LangGraph. She is interested in how the framework can contribute to reducing carbon footprints, optimizing resource usage, and supporting sustainable development goals. Her analysis includes the long-term environmental benefits and cost savings associated with using LangGraph.\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# Inputs\n",
    "max_analysts = 3 \n",
    "topic = \"The benefits of adopting LangGraph as an agent framework\"\n",
    "thread = {\"configurable\": {\"thread_id\": \"1\"}}\n",
    "\n",
    "# Run the graph until the first interruption\n",
    "for event in graph.stream({\"topic\":topic,\n",
    "                           \"max_analysts\":max_analysts}, \n",
    "                          thread, \n",
    "                          stream_mode=\"values\"):\n",
    "    \n",
    "    analysts = event.get('analysts', '')\n",
    "    if analysts:\n",
    "        for analyst in analysts:\n",
    "            print(f\"Name: {analyst.name}\")\n",
    "            print(f\"Affiliation: {analyst.affiliation}\")\n",
    "            print(f\"Role: {analyst.role}\")\n",
    "            print(f\"Description: {analyst.description}\")\n",
    "            print(\"-\" * 50)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ac521a5f-5a4f-44f9-8af9-d05228e20882",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'configurable': {'thread_id': '1',\n",
       "  'checkpoint_ns': '',\n",
       "  'checkpoint_id': '1effd63a-e04f-624d-8002-f29ca1a2a985'}}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We now update the state as if we are the human_feedback node\n",
    "graph.update_state(thread, {\"human_analyst_feedback\": \n",
    "                                \"Add in the CEO of gen ai native startup\"}, as_node=\"human_feedback\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a3be311f-62ee-49e7-b037-75c53d8960a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Dr. Emily Carter\n",
      "Affiliation: Tech Innovators Inc.\n",
      "Role: Technology Adoption Specialist\n",
      "Description: Dr. Carter focuses on the strategic benefits of adopting new technologies like LangGraph. She is particularly interested in how LangGraph can streamline processes, improve efficiency, and provide a competitive edge to organizations. Her analysis often includes case studies and data-driven insights to support the adoption of innovative frameworks.\n",
      "--------------------------------------------------\n",
      "Name: Mr. Raj Patel\n",
      "Affiliation: Data Security Solutions\n",
      "Role: Cybersecurity Analyst\n",
      "Description: Mr. Patel is concerned with the security implications of adopting new frameworks such as LangGraph. His focus is on understanding how LangGraph can enhance or compromise data security within organizations. He evaluates the framework's security features, potential vulnerabilities, and compliance with industry standards.\n",
      "--------------------------------------------------\n",
      "Name: Ms. Sarah Lee\n",
      "Affiliation: GreenTech Ventures\n",
      "Role: Sustainability Consultant\n",
      "Description: Ms. Lee examines the environmental impact and sustainability benefits of adopting LangGraph. She is interested in how the framework can contribute to reducing carbon footprints, optimizing resource usage, and supporting sustainable development goals. Her analysis includes the long-term environmental benefits and cost savings associated with using LangGraph.\n",
      "--------------------------------------------------\n",
      "Name: Dr. Emily Carter\n",
      "Affiliation: Tech Innovators Inc.\n",
      "Role: AI Framework Specialist\n",
      "Description: Dr. Carter focuses on the technical advantages of adopting LangGraph as an agent framework. She is particularly interested in how LangGraph can enhance the efficiency and scalability of AI systems. Her analysis includes a deep dive into the framework's architecture and its potential to streamline complex AI operations.\n",
      "--------------------------------------------------\n",
      "Name: Michael Thompson\n",
      "Affiliation: GreenTech Solutions\n",
      "Role: Sustainability Analyst\n",
      "Description: Michael examines the environmental benefits of using LangGraph. He is concerned with how adopting this framework can lead to more energy-efficient AI processes, reducing the carbon footprint of tech companies. His analysis highlights the importance of sustainable practices in AI development and how LangGraph aligns with these goals.\n",
      "--------------------------------------------------\n",
      "Name: Sophia Zhang\n",
      "Affiliation: AI Pioneers\n",
      "Role: CEO of Gen AI Native Startup\n",
      "Description: Sophia provides insights from a business perspective, focusing on how LangGraph can drive innovation and competitive advantage for startups. She is interested in the framework's ability to accelerate product development and improve market responsiveness. Her analysis considers the strategic implications of adopting LangGraph for emerging AI companies.\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# Check\n",
    "for event in graph.stream(None, thread, stream_mode=\"values\"):\n",
    "    analysts = event.get('analysts', '')\n",
    "    if analysts:\n",
    "        for analyst in analysts:\n",
    "            print(f\"Name: {analyst.name}\")\n",
    "            print(f\"Affiliation: {analyst.affiliation}\")\n",
    "            print(f\"Role: {analyst.role}\")\n",
    "            print(f\"Description: {analyst.description}\")\n",
    "            print(\"-\" * 50)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "0af41f54-88d9-4597-98b0-444c08322095",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'configurable': {'thread_id': '1',\n",
       "  'checkpoint_ns': '',\n",
       "  'checkpoint_id': '1effd63b-5365-6166-8004-3e3b2521d3f6'}}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Confirm we are happy\n",
    "graph.update_state(thread, {\"human_analyst_feedback\": \n",
    "                            None}, as_node=\"human_feedback\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "37123ca7-c20b-43c1-9a71-39ba344e7ca6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--Node--\n",
      "conduct_interview\n",
      "--Node--\n",
      "conduct_interview\n",
      "--Node--\n",
      "conduct_interview\n",
      "--Node--\n",
      "write_conclusion\n",
      "--Node--\n",
      "write_introduction\n",
      "--Node--\n",
      "write_report\n",
      "--Node--\n",
      "finalize_report\n"
     ]
    }
   ],
   "source": [
    "# Continue\n",
    "for event in graph.stream(None, thread, stream_mode=\"updates\"):\n",
    "    print(\"--Node--\")\n",
    "    node_name = next(iter(event.keys()))\n",
    "    print(node_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f8f66ad8-80fd-4eb2-96b6-6ae9dffd060c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "# The Benefits of Adopting LangGraph as an Agent Framework\n",
       "\n",
       "## Introduction\n",
       "\n",
       "LangGraph is revolutionizing the development of AI systems by offering a robust framework that enhances efficiency, scalability, and sustainability. This report explores LangGraph's technical advantages, including its stateful graph architecture that streamlines complex AI operations and its seamless integration with LangChain, which simplifies agent deployment. LangGraph's long-term memory capabilities enable personalized user experiences, while its dynamic workflows support sustainable AI development by reducing energy consumption. For startups, LangGraph accelerates innovation and market responsiveness, providing a competitive edge. This comprehensive analysis highlights why LangGraph is a compelling choice for developers and tech companies alike.\n",
       "\n",
       "---\n",
       "\n",
       "\n",
       "\n",
       "LangGraph is emerging as a transformative agent framework that offers substantial benefits for enhancing AI systems, promoting sustainable development, and providing competitive advantages for startups. Its innovative architecture, centered around stateful graphs, facilitates the management of complex AI operations, enabling seamless transitions between nodes such as agents and tools based on predefined conditions. This robust framework is particularly effective for handling complex workflows, making it a compelling choice for developers aiming to build sophisticated AI applications [1].\n",
       "\n",
       "A key advantage of LangGraph is its integration with LangChain, which simplifies the deployment and operation of AI agents. This integration is crucial for developers who find other frameworks, like OpenAI's, cumbersome to manage without cloud support [2]. LangGraph's architecture supports the creation of Supervisor Agents that can delegate tasks to specialized sub-agents, enhancing scalability and efficient task orchestration [3]. Additionally, LangGraph incorporates long-term memory capabilities, allowing agents to remember previous interactions and user-specific data, which is beneficial for applications requiring personalized user experiences [4].\n",
       "\n",
       "LangGraph also stands out for its contribution to sustainable AI development. By enabling the creation of dynamic, cyclic workflows that adapt to changing conditions, LangGraph enhances the efficiency of AI processes and reduces energy consumption. This adaptability supports environmental sustainability by minimizing waste and optimizing resource usage. Unlike traditional linear workflows, LangGraph's ability to handle cyclic workflows and conditional execution facilitates iterative processing and dynamic decision-making, crucial for developing adaptive AI applications [1]. This flexibility accelerates the development process and ensures that AI systems can be tailored to specific needs, further enhancing their efficiency and sustainability [2].\n",
       "\n",
       "For startups, LangGraph offers a strategic advantage by accelerating innovation and improving market responsiveness. Its graph-based orchestration, persistent state management, and multi-agent coordination make it particularly suited for advanced applications. LangGraph supports dynamic decision-making and iterative processes, enabling startups to innovate rapidly and respond to market changes effectively [1]. The framework's ability to facilitate the development of AI agents that understand and generate human language effectively is crucial for creating innovative applications across various industries [2]. By streamlining the development process, LangGraph allows startups to bring products to market faster, enhancing their competitive positioning in the AI space [3]. The integration with LangChain and LangSmith further accelerates the development and deployment of AI applications, enabling startups to iterate and innovate at an unmatched pace [4].\n",
       "\n",
       "In summary, LangGraph enhances AI system efficiency and scalability through its stateful graph architecture, seamless integration with LangChain, and advanced memory management capabilities. It supports sustainable AI development by reducing energy consumption and optimizing resource usage. For startups, LangGraph provides a framework that supports rapid innovation and market responsiveness, offering a competitive edge in the fast-paced world of artificial intelligence.\n",
       "\n",
       "\n",
       "---\n",
       "\n",
       "## Conclusion\n",
       "\n",
       "LangGraph stands out as a transformative agent framework that significantly enhances the efficiency, scalability, and sustainability of AI systems. Its stateful graph architecture and seamless integration with LangChain streamline complex AI operations, making it an ideal choice for developers seeking robust solutions. By enabling dynamic, cyclic workflows, LangGraph not only optimizes resource usage but also supports environmental sustainability. For startups, LangGraph accelerates innovation and market responsiveness, providing a competitive edge through its advanced capabilities in managing complex workflows and facilitating rapid product development. Overall, LangGraph offers a compelling framework for building sophisticated, sustainable, and innovative AI applications.\n",
       "\n",
       "## Sources\n",
       "[1] https://medium.com/@shravankoninti/langgraph-and-agents-application-3134ef650998  \n",
       "[2] https://www.reddit.com/r/LangChain/comments/1g1pkki/openais_new_framework_for_agents_why_is_langgraph/  \n",
       "[3] https://www.relari.ai/blog/ai-agent-framework-comparison-langgraph-crewai-openai-swarm  \n",
       "[4] https://www.getzep.com/ai-agents/langgraph-tutorial  \n",
       "[5] https://blog.spheron.network/build-smarter-ai-agents-in-minutes-with-langgraph  \n",
       "[6] https://medium.com/@Shamimw/langgraph-simplified-how-to-build-ai-workflows-the-smart-way-791c17749663  \n",
       "[7] https://www.getzep.com/ai-agents/langchain-agents-langgraph  \n",
       "[8] https://www.rapidinnovation.io/post/ai-agents-in-langgraph  \n",
       "[9] https://www.reddit.com/r/LangChain/comments/1eh0ly3/spoke_to_22_langgraph_devs_and_heres_what_we_found/  \n",
       "[10] https://www.langchain.com/customer"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import Markdown\n",
    "final_state = graph.get_state(thread)\n",
    "report = final_state.values.get('final_report')\n",
    "Markdown(report)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9bf8edd-fb42-496c-9bdb-3f5d7b4d79d3",
   "metadata": {},
   "source": [
    "We can look at the trace:\n",
    "\n",
    "https://smith.langchain.com/public/2933a7bb-bcef-4d2d-9b85-cc735b22ca0c/r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "808bd094",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
